# Presented By ZhaoEnxing
# time: 2021/10/20 10:14
# 某糖果生产基地,生产的标准是每袋糖果的净重为500(克)。今从一批压产中抽出10袋,实际测得每袋糖果的净重(克)为:
# 512 503 498 507 496 489 499 501 496 506
# 给定显著性水平α=0.01,试问该批的生产是否正常?假设糖果重量服从正态分布。
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

dataSer = pd.Series([512, 503, 498, 507, 496, 489, 499, 501, 496, 506])
# H0:u >= 500  H1: u < 500
A_mean = dataSer.mean()  # 计算平均值
sample_mean = dataSer.mean()  # 计算平均值
sample_std = dataSer.std()  # 计算标准差
print('样本平均值=', sample_mean, 'g')
print('样本标准差=', sample_std, 'g')
import seaborn as sns

# 解决画图中文乱码
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 绘图
sns.displot(dataSer)
plt.title('数据集分布')
plt.show()
res = stats.ttest_1samp(a=dataSer, popmean=500)
print("t statistic: ", res.statistic)
print("P Value: ", res.pvalue)
a = stats.shapiro(dataSer)
mean = 500  # 假设
b = stats.ttest_1samp(dataSer, mean)
print(a)
print(b)
p = res.pvalue / 2
t = res.statistic
alpha = 0.01
if t < 0 and p < a:
    print("拒绝原假设，有显著性差异，生产不正常")
else:
    print("接受原假设，没有显著性差异，符合政策胜场标准")
